package net.goutalk.glcs.common.sms;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import net.goutalk.glcs.common.enums.SmsType;
import net.goutalk.glcs.config.GlcsSmsConfig;
import lombok.AllArgsConstructor;
import org.dromara.sms4j.aliyun.config.AlibabaConfig;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.config.SupplierFactory;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.huawei.config.HuaweiConfig;
import org.dromara.sms4j.provider.enumerate.SupplierType;
import org.dromara.sms4j.tencent.config.TencentConfig;
import org.springframework.stereotype.Component;


@Component
@AllArgsConstructor
public class SmsSender {

    /**
     * 发送验证码
     * @param mobile
     * @param code
     */
    public void sendCaptcha(String mobile, String code) {
        setConfig(SmsType.CAPTCHA);
        SmsFactory.refresh(getSupplierType());
        SmsResponse smsResponse = SmsFactory.createSmsBlend(getSupplierType()).sendMessage(mobile, code);
    }

    /**
     * 发送提醒
     * @param mobile
     * @param text
     */
    public void sendNotify(String mobile, String text) {
        setConfig(SmsType.NOTIFY);
        SmsFactory.refresh(getSupplierType());
        SmsResponse smsResponse = SmsFactory.createSmsBlend(getSupplierType()).sendMessage(mobile, StrUtil.isNotBlank(text) ? text : "");
    }

    /**
     * 发送传阅
     * @param mobile
     * @param text
     */
    public void sendCirculated(String mobile, String text) {
        setConfig(SmsType.CIRCULATED);
        SmsFactory.refresh(getSupplierType());
        SmsResponse smsResponse = SmsFactory.createSmsBlend(getSupplierType()).sendMessage(mobile, StrUtil.isNotBlank(text) ? text : "");
    }

    /**
     * 发送超时
     * @param mobile
     * @param text
     */
    public void sendTimeout(String mobile, String text) {
        setConfig(SmsType.TIMEOUT);
        SmsFactory.refresh(getSupplierType());
        SmsResponse smsResponse = SmsFactory.createSmsBlend(getSupplierType()).sendMessage(mobile, StrUtil.isNotBlank(text) ? text : "");
    }



    private SupplierType getSupplierType() {
        GlcsSmsConfig glcsSmsConfig = SpringUtil.getBean(GlcsSmsConfig.class);
        switch (glcsSmsConfig.getPlatform()) {
            case ALI_CLOUD:
                return SupplierType.ALIBABA;
            case TENCENT_CLOUD:
                return SupplierType.TENCENT;
            case HW_CLOUD:
                return SupplierType.HUAWEI;
            case HEYI_CLOUD:
                return SupplierType.UNI_SMS;
            case JD_CLOUD:
                return SupplierType.JD_CLOUD;
            case RONGLIAN_CLOUD:
                return SupplierType.CLOOPEN;
            case YIMEI_CLOUD:
                return SupplierType.EMAY;
            case TIANYI_CLOUD:
                return SupplierType.CTYUN;
            case YUNPIAN_CLOUD:
                return SupplierType.YUNPIAN;
            default:
                throw new RuntimeException("未配置短信服务供应商信息: glcs.sms");
        }
    }

    private void setConfig(SmsType type) {
        GlcsSmsConfig glcsSmsConfig = SpringUtil.getBean(GlcsSmsConfig.class);

        switch (glcsSmsConfig.getPlatform()) {
            case ALI_CLOUD:
                AlibabaConfig alibabaConfig = SupplierFactory.getAlibabaConfig();
                break;

            case TENCENT_CLOUD:
                TencentConfig tencentConfig = SupplierFactory.getTencentConfig();
                break;
            case HW_CLOUD:
                if(type == SmsType.CAPTCHA){
                    HuaweiConfig huaweiConfig = SupplierFactory.getHuaweiConfig();
                    huaweiConfig.setSender(glcsSmsConfig.getCaptchaSender());
                    huaweiConfig.setTemplateId(glcsSmsConfig.getCaptchaTemplateId());
                }
                if(type == SmsType.NOTIFY){
                    HuaweiConfig huaweiConfig = SupplierFactory.getHuaweiConfig();
                    huaweiConfig.setSender(glcsSmsConfig.getNotifySender());
                    huaweiConfig.setTemplateId(glcsSmsConfig.getNotifyTemplateId());
                }
                if(type == SmsType.CIRCULATED){
                    HuaweiConfig huaweiConfig = SupplierFactory.getHuaweiConfig();
                    huaweiConfig.setSender(glcsSmsConfig.getCirculatedSender());
                    huaweiConfig.setTemplateId(glcsSmsConfig.getCirculatedTemplateId());
                }
                if(type == SmsType.TIMEOUT){
                    HuaweiConfig huaweiConfig = SupplierFactory.getHuaweiConfig();
                    huaweiConfig.setSender(glcsSmsConfig.getTimeoutSender());
                    huaweiConfig.setTemplateId(glcsSmsConfig.getTimeoutTemplateId());
                }


                break;
            case HEYI_CLOUD:
                SupplierFactory.getUniConfig();
                break;
            case JD_CLOUD:
                SupplierFactory.getJdCloudConfig();
                break;
            case RONGLIAN_CLOUD:
                SupplierFactory.getCloopenConfig();
                break;
            case YIMEI_CLOUD:
                SupplierFactory.getEmayConfig();
                break;
            case TIANYI_CLOUD:
                SupplierFactory.getCtyunConfig();
                break;
            case YUNPIAN_CLOUD:
                SupplierFactory.getYunpianConfig();
                break;
            default:
                throw new RuntimeException("未配置短信服务供应商信息: glcs.sms");
        }
    }

}
